package com.dhemery.mack.adapters.tests;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.junit.Before;
import org.junit.Test;

import com.dhemery.mack.adapters.EditorLauncher;
import com.dhemery.mack.adapters.NodeAdapterFactory;
import com.dhemery.mack.adapters.NodeEditorInput;
import com.dhemery.mack.gui.NodeEditor;
import com.dhemery.mack.model.Node;
import com.dhemery.mack.model.Project;

import static org.mockito.Mockito.*;

public class EditorLauncherTests {

	private IWorkbenchPage workbenchPage;
	private NodeAdapterFactory factory;
	private EditorLauncher launcher;

	@Before
	public void setUp() {
		workbenchPage = mock(IWorkbenchPage.class);
		factory = mock(NodeAdapterFactory.class);
		launcher = new EditorLauncher(workbenchPage, factory);
	}

	@Test
	public void callsPageOpenEdit() throws PartInitException {
		launcher.edit(new Project());
		verify(workbenchPage).openEditor((IEditorInput) anyObject(), anyString());
	}

	@Test
	public void passesNodeEditorIdToPageOpenEdit() throws PartInitException {
		launcher.edit(new Project());
		verify(workbenchPage).openEditor((IEditorInput) anyObject(), eq(NodeEditor.ID));
	}

	@Test
	public void passesNodeAndIEditorInputClassToFactoryGetAdapter() {
		Node node = new Project();

		launcher.edit(node);

		verify(factory).getAdapter(node, IEditorInput.class);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void passesInputFromFactoryToPageOpenEdit() throws PartInitException {
		IEditorInput input = mock(NodeEditorInput.class);

		stub(factory.getAdapter(anyObject(), (Class)anyObject())).toReturn(input);

		launcher.edit(new Project());

		verify(workbenchPage).openEditor(same(input), anyString());
	}
}
